/**
 * 
 */
package com.ansitech.weixin4j.example.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.alipay4j.Alipay;
import org.alipay4j.AlipayBuilder;
import org.alipay4j.AlipayException;
import org.alipay4j.component.life.AccountComponent;
import org.alipay4j.component.life.AdvertComponent;
import org.alipay4j.component.life.DataComponent;
import org.alipay4j.component.life.ExtensionComponent;
import org.alipay4j.component.life.GroupComponent;
import org.alipay4j.component.life.LabelComponent;
import org.alipay4j.component.life.LifeAgentComponent;
import org.alipay4j.component.life.LifeSettingComponent;
import org.alipay4j.component.life.MenuComponent;
import org.alipay4j.component.life.MessageComponent;
import org.alipay4j.component.life.QrcodeComponent;
import org.alipay4j.component.life.SceneComponent;
import org.alipay4j.component.life.TopicComponent;
import org.alipay4j.component.life.UserComponent;
import org.alipay4j.component.life.UserLabelComponent;
import org.alipay4j.component.util.BaseComponent;
import org.alipay4j.component.util.SnsComponent;
import org.alipay4j.model.sns.SnsUser;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 贺亮
 * 支付宝接入
 */
@Controller
@RequestMapping("alipay")
public class AlipayJieruController {

	/**
	 * 重定向到支付宝授权页面
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("oauth")
    public void oauth(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Alipay alipay = AlipayBuilder.newInstance().build();
		String redirectUri = null;
		try {
			redirectUri = alipay.sns().getUserOAuthUrl("auth_base", "LOGIN");
			response.sendRedirect(redirectUri);
		} catch (AlipayException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 支付宝授权回调
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("oauthUri")
	@ResponseBody
    public Object oauthUri(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String auth_code = request.getParameter("auth_code");
		//获得用户信息
		Alipay alipay = AlipayBuilder.newInstance().build();
		SnsUser user = null;
		try {
			user = alipay.sns().getSnsUserByCode(auth_code);
		} catch (AlipayException e) {
			e.printStackTrace();
		}
		return user;
	}
	
	
	@RequestMapping("test")
    @ResponseBody
    public Object test(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得用户信息
        Alipay alipay = AlipayBuilder.newInstance().build();
        /** 返回基础授权组件 */
        BaseComponent baseComponent = alipay.base();
        /** 返回授权相关组件 */
        SnsComponent snsComponent = alipay.sns();
        /** 生活号账户操作组件 */
        AccountComponent accountComponent = alipay.account();
        /** 返回用户组件 */
        UserComponent userComponent = alipay.user();
        /** 广告位操作组件 */
        AdvertComponent advertComponent = alipay.advert();
        /** 数据统计组件 */
        DataComponent dataComponent = alipay.data();
        /** 扩展区组件  */
        ExtensionComponent extensionComponent = alipay.extension();
        /** 分组操作组件 */
        GroupComponent groupComponent = alipay.group();
        /** 标签操作组件 */
        LabelComponent labelComponent = alipay.label();
        /** 生活号操作组件 */
        LifeAgentComponent lifeAgentComponent = alipay.lifeAgent();
        /** 生活号设置组件 */
        LifeSettingComponent lifeSettingComponent = alipay.lifeSetting();
        /** 目录操作组件 */
        MenuComponent menuComponent = alipay.menu();
        /** 消息操作组件 */
        MessageComponent messageComponent = alipay.message();
        /** 二维码操作组件 */
        QrcodeComponent qrcodeComponent = alipay.qrcode();
        /** 场景操作组件 */
        SceneComponent sceneComponent = alipay.scene();
        /** 营销位操作组件  */
        TopicComponent topicComponent = alipay.topic();
        /** 用户标签操作组件 */
        UserLabelComponent userLabelComponent = alipay.userLabel();
        return null;
    }
}
